package com.utn.tp.integrador.widget;

import java.lang.reflect.Method;

import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.content.Context;
import android.content.Intent;
import android.media.AudioManager;
import android.net.ConnectivityManager;
import android.net.wifi.WifiManager;
import android.provider.Settings;

public class ControladorWidget {

	Context context;

	public ControladorWidget(Context context) {
		super();
		this.context = context;
	}

	public void wifi(Activity dummy) {

		WifiManager wifiManager = (WifiManager) context
				.getSystemService(Context.WIFI_SERVICE);

		if (wifiManager.isWifiEnabled()) {
			wifiManager.setWifiEnabled(false);

		} else
			wifiManager.setWifiEnabled(true);

	}

	public void modoAvion() {
		boolean isEnabled = Settings.System.getInt(
				context.getContentResolver(), Settings.System.AIRPLANE_MODE_ON,
				0) == 1;

		Settings.System.putInt(context.getContentResolver(),
				Settings.System.AIRPLANE_MODE_ON, isEnabled ? 0 : 1);

		Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
		intent.putExtra("state", !isEnabled);
		context.sendBroadcast(intent);
	}

	public void TresG(boolean enabled) {
		try {
			ConnectivityManager mgr = (ConnectivityManager) context
					.getSystemService(Context.CONNECTIVITY_SERVICE);
			Method dataMtd = ConnectivityManager.class.getDeclaredMethod(
					"setMobileDataEnabled", boolean.class);
			dataMtd.setAccessible(true);
			if (mgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE)
					.isConnectedOrConnecting()) {
				dataMtd.invoke(mgr, false);
			} else {
				dataMtd.invoke(mgr, true);
			}
		} catch (Exception e) {

		}

	}

	public void audio() {
		AudioManager aManager = (AudioManager) context
				.getSystemService(Context.AUDIO_SERVICE);
		if (aManager.getRingerMode() == AudioManager.RINGER_MODE_NORMAL) {
			aManager.setRingerMode(AudioManager.RINGER_MODE_SILENT);
		} else {
			aManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
		}
	}

	public void bluetooth() {
		BluetoothAdapter mBluetoothAdapter = BluetoothAdapter
				.getDefaultAdapter();
		if (mBluetoothAdapter.isEnabled()) {
			mBluetoothAdapter.disable();
		} else {
			mBluetoothAdapter.enable();
		}
	}
}
